--#include "data\config\answer\AnswerBank.txt" once   --题库

AnswerType_DailyActivity 	=	1		--每日活动答题
AnswerType_DailyPerson 		=	2		--每日个人答题（金币）

ANSWER_RESULT_ERROR  	= 0 			--答题错误
ANSWER_RESULT_OK  		= 1 			--答题正确
ANSWER_RESULT_TIMEOUT 	= 2 			--答题超时

ANSWER_OP_HITRIGHT		= 1				--一击必中
ANSWER_OP_EXCLUDE		= 2				--答案排错
ANSWER_OP_DOUBLE		= 3				--双倍奖励

ANSWER_RANK_ACTIVITY_DAILY   	= 1			--每日答题的日榜
ANSWER_RANK_ACTIVITY_WEEKLY  	= 2			--每日答题的周榜
ANSWER_RANK_ACTIVITY_DATA  		= 3			--每日答题个人数据榜


--[[
从题库中随机抽取N条
]]
function CommonAnswerDrawAnswersInBank( bankIdx, num )
	--print("CommonAnswerDrawAnswersInBank,  bankIdx="..bankIdx..", num")
	local answerBank = AnswerBank[bankIdx]
	return DistributeElem( answerBank, num, false)
end

--[[
从题库中随机抽取N条,
只返回题目的idx
]]
function CommonAnswerDrawAnswersIdxInBank( bankIdx, num )
	--print("CommonAnswerDrawAnswersIdxInBank,  bankIdx="..bankIdx..", num="..num)
	local answerBank 	= AnswerBank[bankIdx]
	if not answerBank then
		return {}
	end

	local answerBankCopy 	= DeepCopyFromObject( answerBank )   	--深度拷贝一个副本（不要原对象）
	local answers 			= DistributeElemKey( answerBankCopy, num, false)
	local answersIdx 		= {}
	for k, answer in pairs( answers ) do
		--print("CommonAnswerDrawAnswersIdxInBank,  k="..k..", question="..answer.question)
		table.insert( answersIdx, k )
	end
	return answersIdx
end

--[[
随机抽取一道题，返回元素本身
]]
function CommonAnswerDrawOneAnswer( bankIdx ) 
	local answerBank 	= AnswerBank[bankIdx]
	if not answerBank then
		return {}
	end
	return GetItemIdxRand( answerBank )
end

function GetCommonAnswerQuestionByIdx( bankIdx, idx )
	--print("GetCommonAnswerQuestionByIdx, bankIdx="..bankIdx..", idx="..idx)
	local answerBank = AnswerBank[bankIdx]
	if not answerBank then
		return nil
	end

	return answerBank[idx]
end

--[[
获取问题的答案IDX
]]
function GetCommonAnswerRightAnswerIdx( question )
	for answerIdx, answer in ipairs( question.answers ) do
		if answer[2] == true then
			return answerIdx
		end
	end
	return 0
end

--[[
下发题目
closePanel:是否关闭倒计时面板
]]
function SendCommonAnswerQuestion( sysarg, activityId, roundIdx, lastTime, question, score, closePanel )
	DoSendCommonAnswerQuestion( sysarg, activityId, roundIdx, lastTime, question, score )
	if closePanel then
		CloseBoardPanel( sysarg )
	end
end


function DoSendCommonAnswerQuestion( sysarg, activityId, roundIdx, lastTime, question, score )
	if not question then
		return
	end

	local npack = DataPack.allocPacket(sysarg, enDefaultEntitySystemID, sSendCommonAnswerQuestion)
	if not npack then
		return
	end

	DataPack.writeShort(npack, activityId)	
	DataPack.writeShort(npack, roundIdx)
	DataPack.writeShort(npack, lastTime)
	DataPack.writeShort(npack, score)
	DataPack.writeString(npack, question.question or "" )			--没有配置语言包
	--print("DoSendCommonAnswerQuestion, roundIdx="..roundIdx..", lastTime="..lastTime..", question="..question.question..", score="..score)
	DataPack.writeByte(npack, #question.answers)
	for ansIdx, ans in ipairs( question.answers ) do
		local answerStr = ans[1] or "" 							--没有配置语言包
		DataPack.writeByte(npack, ansIdx)
		DataPack.writeString(npack, answerStr)
		--print("	  -> answer, ansIdx="..ansIdx..", answerStr="..answerStr)
	end
	DataPack.flush(npack)
end

--[[
下发答题结果
actorAnswerIdx：0-表示玩家没有给出答案
rightAnswerIdx：0-表示配置错误
]]
function SendCommonAnswerCheckResult( sysarg, activityId, roundIdx, actorAnswerIdx, rightAnswerIdx, score )
	if not score then
		score = 0
	end
	local result = DoSendCommonAnswerCheckResult( sysarg, activityId, roundIdx, actorAnswerIdx, rightAnswerIdx )
	SendCommonAnswerCheckBoardMsg( sysarg, result, score )
	return result
end


function DoSendCommonAnswerCheckResult( sysarg, activityId, roundIdx, actorAnswerIdx, rightAnswerIdx )
	local npack = DataPack.allocPacket(sysarg, enDefaultEntitySystemID, sSendCommonAnswerCheckResult)
	if not npack then
		return
	end

	local result = 0
	if actorAnswerIdx == 0 then				--答题超时
		result = ANSWER_RESULT_TIMEOUT
	elseif rightAnswerIdx > 0 and actorAnswerIdx == rightAnswerIdx then		--答题正确
		result = ANSWER_RESULT_OK
	else 									--答题错误
		result = ANSWER_RESULT_ERROR
	end

	DataPack.writeShort(npack, activityId)	
	DataPack.writeShort(npack, roundIdx)
	DataPack.writeByte(npack, actorAnswerIdx)
	DataPack.writeByte(npack, rightAnswerIdx)
	DataPack.writeByte(npack, result)
	DataPack.flush(npack)

	return result
	--print("DoSendCommonAnswerCheckResult, roundIdx="..roundIdx..", actorAnswerIdx="..actorAnswerIdx..", rightAnswerIdx="..rightAnswerIdx..", result="..result)
end


function SendCommonAnswerOpReply( sysarg, activityId, roundIdx, opType, param )
	local npack = DataPack.allocPacket(sysarg, enDefaultEntitySystemID, sSendCommonAnsweOpReply)
	if not npack then
		return
	end

	DataPack.writeShort(npack, activityId)	
	DataPack.writeShort(npack, roundIdx)
	DataPack.writeByte(npack, opType)
	DataPack.writeByte(npack, param)
	DataPack.flush(npack)

	--print("SendCommonAnswerOpReply, activityId="..activityId..", roundIdx="..roundIdx..", opType="..opType..", param="..param)
end

--[[
向场景类玩家广播答题活动进行中的信息
questionIdx:第N轮答题
]]
function BroadCommonAnswerQuestionInArea( activityId, sceneId, answerArea, lastTime, roundIdx, question, score, levelLimit )
	--print("BroadCommonAnswerQuestionInArea, activityId="..activityId..", sceneId="..sceneId..", roundIdx="..roundIdx)
	local scenePtr = Fuben.getSceneById( sceneId, 0 )		--一般不是副本
	local playerList = nil
	if answerArea then			--有答题区域
		local beginPosX	= answerArea[1][1]
		local beginPosY	= answerArea[1][2]
		local width		= answerArea[2][1] - beginPosX
		local heigh		= answerArea[2][2] - beginPosY
		playerList = LuaHelp.getNearEntityListFromRange( scenePtr, beginPosX,beginPosY, width, heigh, enActor) or {}
	else
		playerList = LuaHelp.getSceneActorListById( sceneId )
	end
	
	local boardStr 	= Lang.ScriptTips.SceneAnswer005
	local needCircle = levelLimit[1] or 0
	local needLevel  = levelLimit[2] or 0
	for i, player in ipairs( playerList or {} ) do
		if Actor.checkActorLevel( player, needLevel,  needCircle ) then
			SendCommonAnswerQuestion( player, activityId, roundIdx, lastTime, question, score, true )
			SendCommonRankSelfData( player, enCommonRankType_AnswerToday )
			OpenBoardPanel( player, enBoardPanelStyle_CountDown1, boardStr, lastTime+1 )
			--[[
			SendAnswerRankData(player, AnswerType_DailyActivity, ANSWER_RANK_ACTIVITY_DAILY, 10, DailyAnswerCfg.rankLimit)
			SendDailyAnswerAllRightData( player )			--全服答题
			if needRightNum > 0 then
				Actor.sendTipmsg( player, string.format(Lang.Script.Answer033, needRightNum), ttChatWindow )
			end
			]]
		end
	end
	--print("BroadDailyAnswerActivityDoingInArea, playerNum="..#playerList)
end

--[[
玩家答题积分变化
rankType：参考 enum enCommonRankType
addPoint:>0-增加，<0-减少
]]
function UpdateAnswerRankPoint( sysarg, rankType, addPoint )
	--print("UpdateAnswerRankPoint, rankType="..rankType..", addPoint="..addPoint)
	local rankCfg = GetCommonRankCfgByType( rankType )
	if not rankCfg then
		return
	end

	local rankName = rankCfg.rankName
	if not rankName then
		return
	end
	--print("UpdateAnswerRankPoint, rankName="..rankName..", addPoint="..addPoint)

	local actorId 	= Actor.getActorId( sysarg )
	local actorName = Actor.getName( sysarg )
	local job 		= Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION)
	UpdateCommonRankValueById( actorId, rankName, addPoint )
	SetCommonRankActorData( rankName, actorId, actorName, job )
	SendCommonRankSelfData( sysarg, rankType )
end

function SendCommonAnswerCheckBoardMsg( sysarg, result, score )
	--print("SendCommonAnswerCheckBoardMsg, result="..result..", score="..score)
	local boardStr 		= nil
	local broadStyle    = enBoardPanelStyle_Default
	if result == ANSWER_RESULT_OK then
		boardStr 	= string.format( Lang.ScriptTips.AnswerActivity004, score )
		broadStyle	= enBoardPanelStyle_Right
	else
		boardStr = Lang.ScriptTips.AnswerActivity005
		broadStyle	= enBoardPanelStyle_Error
	end
	OpenBoardPanel( sysarg, broadStyle, boardStr, 0 )
end
